//
//  BlackPersonListVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/4/22.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "BlackPersonListVC.h"

@interface BlackPersonListVC () {
    int pIntPage;
}

@property(nonatomic,strong) NSMutableArray *theList;
@property (weak, nonatomic) IBOutlet UILabel *lblMessage;

@end

@implementation BlackPersonListVC

- (void)initInfo
{
    [super initInfo];
    pIntPage = 1;
    _theList = [NSMutableArray array];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self selAddLeftBackButton];
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"黑名单"];
    
    [self.pTableView setTableFooterView:[UIView new]];
    
    __weak typeof(self) weakSelf = self;
    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pIntPage = 1;
        [weakSelf requestMessage];
    }];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestMessage];
    }];
    
    [self.pTableView.mj_header beginRefreshing];
    [self.pTableView.mj_footer setHidden:YES];
    
}

- (NSMutableArray *)theListDic{
    if (!_theList) {
        _theList = [NSMutableArray array];
    }
    return _theList;
}

- (void)requestMessage
{
    __weak typeof(self) weakSelf = self;
    [TDNetwork requestGetBlackList:getBlackList_interface
                            andPage:pIntPage
                        andPageSize:10
                           complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary)
     {
         if (succeed) {
             if (pIntPage == 1) {
                 [_theList removeAllObjects];
             }
             NSDictionary *dict = [dictionary valueForKey:@"res"];
             NSLog(@"%@", dict);
             if (dict && [dict isKindOfClass:[NSDictionary class]]) {
                 NSArray *array = [dict objectForKey:@"list"];
                 [weakSelf.theList addObjectsFromArray:array];
                 
                 if (array.count == 10) {
                     pIntPage ++;
                     [self.pTableView.mj_footer setHidden:NO];
                 } else {
                     [self.pTableView.mj_footer setHidden:YES];
                 }
             }
             [weakSelf.pTableView reloadData];
             
         } else {
         }
         
         [self.pTableView.mj_footer endRefreshing];
         [self.pTableView.mj_header endRefreshing];
         
         if (_theList.count == 0) {
             [_lblMessage setHidden:NO];
         } else {
             [_lblMessage setHidden:YES];
         }
     }];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.theList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfBlackPersonListCell"];
    NSDictionary *dict =  [self.theList objectAtIndex:indexPath.row];
    
    UIImageView *imgPic = (UIImageView *)[cell.contentView viewWithTag:1201];
    UIImageView *imgSex = (UIImageView *)[cell.contentView viewWithTag:1202];
    UILabel *lblName = (UILabel *)[cell.contentView viewWithTag:1101];
    UILabel *lblAge = (UILabel *)[cell.contentView viewWithTag:1102];
    UIButton *btnRemove = (UIButton *)[cell.contentView viewWithTag:1301];
    
    [imgPic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/api/common/file/imgGet?fid=%@", IP, [dict objectForKey:@"headImage"]]]];
    [imgPic.layer setCornerRadius:25.0f];
    [imgPic.layer setMasksToBounds:YES];
    [imgSex setImage:[UIImage imageNamed:[[dict objectForKey:@"userSex"] isEqualToString:@"1"]?@"sex_boy":@"sex_girl"]];
    [lblName setText:[NSString stringWithFormat:@"%@", [dict objectForKey:@"nickName"]]];
    [lblAge setText:[NSString stringWithFormat:@"%@", [dict objectForKey:@"userAge"]]];
    if ([[dict objectForKey:@"userSex"] isEqualToString:@"1"]) {
        [lblAge setTextColor:[UIColor colorWithRed:108/255.0f green:181/255.0f blue:255/255.0f alpha:1]];
    } else {
        [lblAge setTextColor:[UIColor colorWithRed:252/255.0f green:110/255.0f blue:108/255.0f alpha:1]];
    }
    [btnRemove addTarget:self action:@selector(btnRemoveAction:) forControlEvents:UIControlEventTouchUpInside];
    [btnRemove setAccessibilityIdentifier:[dict objectForKey:@"uid"]];
    [btnRemove.layer setCornerRadius:3.0f];
    [btnRemove.layer setMasksToBounds:YES];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)btnRemoveAction:(UIButton *)sender {
    [SVProgressHUD show];
    [TDNetwork requestDeleteBlackList:deleteBlackList_interface toUid:sender.accessibilityIdentifier complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [SVProgressHUD showSuccessWithStatus:@"移除黑名单成功"];
            [self.pTableView.mj_header beginRefreshing];
        } else {
            [SVProgressHUD showErrorWithStatus:@"操作失败,请重试!"];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
